using System.Configuration;
using System.Xml;

namespace WoltersKluwer.SPChainGang.Core.Configuration
{
	/// <summary>
	/// Handler for a configuration element containing XML markup
	/// </summary>
	public class XmlTextConfigurationElement : ConfigurationElement
	{
		private XmlNode valueXml;
		private static readonly XmlDocument doc;
		static XmlTextConfigurationElement()
		{
			doc = new XmlDocument();
		}
		/// <summary>
		/// Gets or sets the XML value of the node.
		/// </summary>
		/// <value>The XML value.</value>
		protected XmlNode ValueXml
		{
			get
			{
				return this.valueXml;
			}
			set
			{
				this.valueXml = value;
			}
		}
		/// <summary>
		/// Reads XML from the configuration file.
		/// </summary>
		/// <param name="reader">The <see cref="T:System.Xml.XmlReader"></see> that reads from the configuration file.</param>
		/// <param name="serializeCollectionKey">true to serialize only the collection key properties; otherwise, false.</param>
		protected override void DeserializeElement(System.Xml.XmlReader reader, bool serializeCollectionKey)
		{
			this.ValueXml = doc.ReadNode(reader);
		}
		/// <summary>
		/// Writes the outer tags of this configuration element to the configuration file when implemented in a derived class.
		/// </summary>
		/// <param name="writer">The <see cref="T:System.Xml.XmlWriter"></see> that writes to the configuration file.</param>
		/// <param name="elementName">The name of the <see cref="T:System.Configuration.ConfigurationElement"></see> to be written.</param>
		/// <returns>true if writing was successful.</returns>
		/// <exception cref="T:System.Exception">The element has multiple child elements. </exception>
		protected override bool SerializeToXmlElement(XmlWriter writer, string elementName)
		{
			if (this.ValueXml == null)
			{
				return false;
			}
			if (writer != null)
			{
				this.ValueXml.WriteTo(writer);
			}
			return true;
		}
		/// <summary>
		/// Gets a unique value representing the <see cref="T:System.Configuration.ConfigurationElement"></see> current instance.
		/// </summary>
		/// <returns>
		/// A unique value representing the <see cref="T:System.Configuration.ConfigurationElement"></see> current instance.
		/// </returns>
		public override int GetHashCode()
		{
			return this.ValueXml.GetHashCode();
		}
		/// <summary>
		/// Modifies the <see cref="T:System.Configuration.ConfigurationElement"></see> object to remove all values that should not be saved.
		/// </summary>
		/// <param name="sourceElement">A <see cref="T:System.Configuration.ConfigurationElement"></see> object at the current level containing a merged view of the properties.</param>
		/// <param name="parentElement">The parent <see cref="T:System.Configuration.ConfigurationElement"></see> object, or null if this is the top level.</param>
		/// <param name="saveMode">A <see cref="T:System.Configuration.ConfigurationSaveMode"></see>  object that determines which property values to include.</param>
		protected override void Unmerge(ConfigurationElement sourceElement, ConfigurationElement parentElement, ConfigurationSaveMode saveMode)
		{
			base.Unmerge(sourceElement, parentElement, saveMode);
			this.ValueXml = ((XmlTextConfigurationElement)sourceElement).ValueXml;
		}
	}
}
